﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class EntityLinkComponent:WorkflowComponentBase
    {
        #region Overrides of WorkflowComponentBase

        private EntityLinkInstruction _Instruction;
        /// <summary>an object that encapsulate instruction settings.</summary>
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction = (EntityLinkInstruction) value; }
        }

        private EntityLinkOutput _Output;
        /// <summary>an object to encapsulate output</summary>
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output=(EntityLinkOutput)value; }
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public EntityLinkComponent()
            : base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.ComponentName = "Link";
            this.Description = "Link entity based on attribute statistics, " +
                               "entity2->entity1 (many to one) relations";
            this.ComponentIcon = Resources.Node;
            this.ComponentImage = Resources.Node48;

            this._Instruction = new EntityLinkInstruction(
                this.ComponentID, Guid.Empty,
                string.Empty, string.Empty, string.Empty, string.Empty,
                new Dictionary<string, string>(), new List<string>(),
                new List<string>(), string.Empty,
                string.Empty, string.Empty, string.Empty, string.Empty,
                new Dictionary<string, string>(), new List<string>(),
                new List<string>(), string.Empty,
                new Dictionary<string, string>(),
                new Dictionary<string, double>(),
                new Dictionary<string, double>(),
                new Dictionary<string, string>(),
                0.30, 0.55, string.Empty, string.Empty,string.Empty);
            this._Output = new EntityLinkOutput(this.ComponentID, Guid.Empty, 0, 0, 0);
        }

        #region src-1
        /// <summary></summary>
        public string ConnStr1
        {
            get
            {
                return this._Instruction.ConnStr1;
            }
            set
            {
                this._Instruction.ConnStr1 = value;
            }
        }

        /// <summary></summary>
        public string SchemaName1
        {
            get
            {
                return this._Instruction.SchemaName1;
            }
            set
            {
                this._Instruction.SchemaName1 = value;
            }
        }

        /// <summary></summary>
        public string TableName1
        {
            get
            {
                return this._Instruction.TableName1;
            }
            set
            {
                this._Instruction.TableName1 = value;
            }
        }

        /// <summary></summary>
        public string Sql1
        {
            get
            {
                return this._Instruction.Sql1;
            }
            set
            {
                this._Instruction.Sql1 = value;
            }
        }

        /// <summary></summary>
        public Dictionary<string, string> FieldTransformations1
        {
            get
            {
                return this._Instruction.FieldTransformations1;
            }
            set
            {
                this._Instruction.FieldTransformations1 = value;
            }
        }

        /// <summary></summary>
        public List<string> DescFields1
        {
            get
            {
                return this._Instruction.DescFields1;
            }
            set
            {
                this._Instruction.DescFields1 = value;
            }
        }

        /// <summary></summary>
        public List<string> AssocFields1
        {
            get
            {
                return this._Instruction.AssocFields1;
            }
            set
            {
                this._Instruction.AssocFields1 = value;
            }
        }

        /// <summary></summary>
        public string PKField1
        {
            get
            {
                return this._Instruction.PKField1;
            }
            set
            {
                this._Instruction.PKField1 = value;
            }
        }
        #endregion

        #region src-2
        /// <summary></summary>
        public string ConnStr2
        {
            get
            {
                return this._Instruction.ConnStr2;
            }
            set
            {
                this._Instruction.ConnStr2 = value;
            }
        }

        /// <summary></summary>
        public string SchemaName2
        {
            get
            {
                return this._Instruction.SchemaName2;
            }
            set
            {
                this._Instruction.SchemaName2 = value;
            }
        }

        /// <summary></summary>
        public string TableName2
        {
            get
            {
                return this._Instruction.TableName2;
            }
            set
            {
                this._Instruction.TableName2 = value;
            }
        }

        /// <summary></summary>
        public string Sql2
        {
            get
            {
                return this._Instruction.Sql2;
            }
            set
            {
                this._Instruction.Sql2 = value;
            }
        }

        /// <summary></summary>
        public Dictionary<string, string> FieldTransformations2
        {
            get
            {
                return this._Instruction.FieldTransformations2;
            }
            set
            {
                this._Instruction.FieldTransformations2 = value;
            }
        }

        /// <summary></summary>
        public List<string> DescFields2
        {
            get
            {
                return this._Instruction.DescFields2;
            }
            set
            {
                this._Instruction.DescFields2 = value;
            }
        }

        /// <summary></summary>
        public List<string> AssocFields2
        {
            get
            {
                return this._Instruction.AssocFields2;
            }
            set
            {
                this._Instruction.AssocFields2 = value;
            }
        }

        /// <summary></summary>
        public string PKField2
        {
            get
            {
                return this._Instruction.PKField2;
            }
            set
            {
                this._Instruction.PKField2 = value;
            }
        }
        #endregion

        #region comparers and weights
        /// <summary></summary>
        public Dictionary<string, string> Field1_Field2_Mappings
        {
            get
            {
                return this._Instruction.Field1_Field2_Mappings;
            }
            set
            {
                this._Instruction.Field1_Field2_Mappings = value;
            }
        }

        /// <summary>field name 1 -> weight</summary>
        public Dictionary<string, double> FieldWeights
        {
            get
            {
                return this._Instruction.FieldWeights;
            }
            set
            {
                this._Instruction.FieldWeights = value;
            }
        }

        /// <summary></summary>
        public Dictionary<string, double> FieldPenalties
        {
            get
            {
                return this._Instruction.FieldPenalties;
            }
            set
            {
                this._Instruction.FieldPenalties = value;
            }
        }

        /// <summary></summary>
        public Dictionary<string, string> FieldComparers
        {
            get
            {
                return this._Instruction.FieldComparers;
            }
            set
            {
                this._Instruction.FieldComparers = value;
            }
        }

        /// <summary></summary>
        public double AssocThreshold
        {
            get
            {
                return this._Instruction.AssocThreshold;
            }
            set
            {
                this._Instruction.AssocThreshold = value;
            }
        }

        /// <summary></summary>
        public double LinkThreshold
        {
            get
            {
                return this._Instruction.LinkThreshold;
            }
            set
            {
                this._Instruction.LinkThreshold = value;
            }
        }
        #endregion

        #region output
        /// <summary></summary>
        public string OutputTableName
        {
            get
            {
                return this._Instruction.OutputTableName;
            }
            set
            {
                this._Instruction.OutputTableName = value;
            }
        }

        /// <summary></summary>
        public string EntityIDField1
        {
            get
            {
                return this._Instruction.EntityIDField1;
            }
            set
            {
                this._Instruction.EntityIDField1 = value;
            }
        }

        /// <summary></summary>
        public string EntityIDField2
        {
            get
            {
                return this._Instruction.EntityIDField2;
            }
            set
            {
                this._Instruction.EntityIDField2 = value;
            }
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);
            this.Instruction.Serialize(ref xDoc, ref xNode);
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            EntityLinkComponent component = (EntityLinkComponent)base.Instantiate(xNode);
            component.Instruction = component.Instruction.Instantiate(xNode.SelectSingleNode("Instruction"));
            return component;
        }
    }
}
